package com.xnew.org.controller;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnew.org.service.OrgService;
import com.xnew.utils.DataInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnew.aes.SecurityParameter;
import com.xnew.exception.CustomException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sysorg-api",method = RequestMethod.POST)
public class OrgController {
	@Autowired
	private OrgService orgService ;

	@ResponseBody
	@RequestMapping(value = "/querySysOrgJson",method = RequestMethod.POST)
	@SecurityParameter
	public String querySysOrgJson(@RequestBody DataInput input, HttpServletRequest request){
		try{
			List<Map<String, Object>> orgList = new ArrayList<Map<String, Object>>();
			orgList = orgService.createOrgTree();
			JSON json = (JSON) JSONObject.toJSON(orgList);
			System.out.print(json.toJSONString());
			return json.toJSONString();
		}catch(Exception e){
			e.printStackTrace();
			throw new CustomException();
		}
	}

	/**
	 * 根据机构编号查询下级机构
	 * */
	@ResponseBody
	@RequestMapping(value = "/querySysOrgJsonByOrgId",method = RequestMethod.POST)
	@SecurityParameter
	public String querySysOrgJsonByOrgId(@RequestBody DataInput input, HttpServletRequest request){
		try{
			String orgId = input.getParam().get("orgId").toString();
			List<Map<String, Object>> orgList = new ArrayList<Map<String, Object>>();
			orgList = orgService.createOrgTreeByOrgId(orgId);
			JSON json = (JSON) JSONObject.toJSON(orgList);
			System.out.print(json.toJSONString());
			return json.toJSONString();
		}catch(Exception e){
			e.printStackTrace();
			throw new CustomException();
		}
	}

	/**
	 * 根据机构编号查询下级机构
	 * */
	@ResponseBody
	@RequestMapping(value = "/querySysUserJsonByOrgId",method = RequestMethod.POST)
	@SecurityParameter
	public String querySysUserJsonByOrgId(@RequestBody DataInput input, HttpServletRequest request){
		try{
			String orgId = input.getParam().get("orgId").toString();
			List<Map<String, Object>> orgList = new ArrayList<Map<String, Object>>();
			orgList = orgService.createOrgTreeByOrgId(orgId);
			JSON json = (JSON) JSONObject.toJSON(orgList);
			System.out.print(json.toJSONString());
			return json.toJSONString();
		}catch(Exception e){
			e.printStackTrace();
			throw new CustomException();
		}
	}

	/**
	 * 根据机构编号查询下级机构
	 * */
	@ResponseBody
	@RequestMapping(value = "/queryFirstNodeByOrgId",method = RequestMethod.POST)
	@SecurityParameter
	public String queryFirstNodeByOrgId(@RequestBody DataInput input, HttpServletRequest request){
		try{
			String orgId = input.getParam().get("orgId").toString();
			List<Map<String, Object>> orgList = new ArrayList<Map<String, Object>>();
			orgList = orgService.queryFirstNodeByOrgId(orgId);
			JSON json = (JSON) JSONObject.toJSON(orgList);
			System.out.print(json.toJSONString());
			return json.toJSONString();
		}catch(Exception e){
			e.printStackTrace();
			throw new CustomException();
		}
	}


}
